﻿using AntdUI;
using System.Drawing.Printing;
using DataAcquisitionBLL;
using DataAcquisitionDAL;
using Microsoft.IdentityModel.Tokens;

namespace DataAcquisition
{
    public partial class DevicePointManageForm : BaseForm
    {
        #region 属性

        /// <summary>
        /// 设备编号
        /// </summary>
        public uint DeviceNumber;
        /// <summary>
        /// 设备
        /// </summary>
        private DeviceModel Device = new DeviceModel();

        /// <summary>
        /// 逻辑处理
        /// </summary>
        private DevicePoint devicePoint = new DevicePoint();

        #endregion

        #region 自定义方法
        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<DevicePointModel> list = devicePoint.DevicePointList(DeviceNumber);
            UpdateTable(list);
        }

        /// <summary>更新Table
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<DevicePointModel> list)
        {
            var antItemList = new List<DevicePointItem>();
            foreach (DevicePointModel item in list)
            {
                string decrypt = Encryption.DesDecrypt(item.Address, DevicePoint.key);
                if (!decrypt.IsNullOrEmpty())
                {
                    string range = item.RangeMin.ToString() + "-" + item.RangeMax.ToString();
                    string outValue = item.OutMin.ToString() + "-" + item.OutMax.ToString();
                    string alarm = item.AlarmMin.ToString() + "-" + item.AlarmMax.ToString();
                    string pointType = item.PointType;
                    antItemList.Add(new DevicePointItem(item.DeviceNumber, item.DeviceName, item.Number,
                                                        item.Name, decrypt, item.PointType, range, outValue, alarm,
                                                        item.ErrorRange.ToString(), item.Amend.ToString(), item.Unit));
                }
                else
                {
                    Log.Info(string.Format("【{0}】点位地址解密失败", item.Number));
                }
            }
            Table1.DataSource = antItemList;
        }
        #endregion

        #region 系统方法
        public DevicePointManageForm()
        {
            InitializeComponent();
            Header.Text = "点位管理";
            FullScreen = true;
        }

        private void DevicePointManageForm_Load(object sender, EventArgs e)
        {
            Table1.Height = Height - 80 - 128;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("DeviceNumber","设备编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DeviceName","设备名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Number","点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Name","点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Address","点位地址",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PointType","点位类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Range","量程范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("OutValue","输出范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Alarm","报警范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ErrorRange","判断范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Amend","修正数值",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Unit","数据单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Operation","操作",AntdUI.ColumnAlign.Center){ Width="120", LineBreak=true}
            };
            Response response = devicePoint.CheckDeviceState(DeviceNumber);
            if (response.Code != 200)
            {
                if (this.Owner is not null)
                {
                    this.Close();
                    DeviceManageForm frm = (DeviceManageForm)this.Owner;
                    frm.Error(response.Msg);
                    frm.LoadList();
                    return;
                }
            }
            else
            {
                Device = (DeviceModel)response.Data;
                Header.Text = string.Format("【{0}】点位管理", Device.Name);
                Table1.Spin("正在加载中...", (config) =>
                {
                    LoadList();
                });
            }
        }

        #endregion

        #region 响应事件
        private void AddButton_Click(object sender, EventArgs e)
        {
            this.Invoke(new MethodInvoker(delegate
            {
                DevicePointAddForm frm = new DevicePointAddForm();
                frm.DeviceObject = Device;
                frm.Owner = this;
                frm.ShowDialog();
            }));
        }
        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is DevicePointItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    string msg = string.Format("您确定要删除【{0}】点位吗？", data.Name);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            Response response = devicePoint.DeleteDevicePoint(data.Number);
                            if (response.Code != 200)
                            {
                                Error(response.Msg);
                            }
                            else
                            {
                                Success(response.Msg);
                                Task.Run(() =>
                                {
                                    LoadList();
                                });
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
